<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
//扫码上传小程序，先进入上传页面读秒，由上传页面发起获取二维码请求，获取成功后结束读秒，显示二维码。并提醒客户授权成功后点击按钮查询上传结果。
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
//$return['htmls']=file_get_contents(ROOT_D.'/manage/templates/default/windows/baiduapp/up.html');
$id=intval($_RQ['id']);
if(!$id){
	exi('请先选择要上传的小程序！','error');
}
if($_RQ['op']=='get'){
	$return['id']=$id;
	$last=pdo_getcolumn('core_account_baiduapp',array('id'=>$id),'last_up');
	if(TIMESTAMP-60<$last){
		exi('请勿频繁上传小程序！稍后再上传！','error','/baiduapp.list/');
	}
	pdo_update('core_account_baiduapp',array('last_up'=>TIMESTAMP),array('id'=>$id));
	$return['item']=array();
	exi($return);
}
if($_RQ['op']=='get_ewm'){
	$item=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item['appid'] || !$item['title']){
			exi('请先设置好：小程序相关参数！','error','goback');
		}
		if(substr($item['url'],11)=='./index.php'){
			$pre=get_account_url();
			$item['url']=$pre.'custom'.substr($item['url'],1);
		}
	if(!$item['items'] && !$item['url']){
		exi('请先设计好再提交发布！','error');
	}
		$params=array(
			'appid'=>$item['appid'],
			'type'=>$item['type'],
			'is_wz'=>$item['is_wz'],
			'title'=>$item['title'],
			'desc'=>$item['desc'],
			'logo'=>tomedia($item['logo']),
			'textcolor'=>$item['textcolor'],
			'bgcolor'=>$item['bgcolor'],
			'siteroot'=>get_account_url(),
			'uniacid'=>$_SESSION['uniacid'],
			'baiduapp_id'=>$item['id'],
			'att_url'=>ATTACHMENT_URL,
			'items'=>$item['items'],
			'setting'=>$item['setting'],
			'url'=>$item['url'],
			'web_id'=>$item['web_id'],
		);
		$menu=pdo_getcolumn('core_diy_menu',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'plugin'=>'core'),'data');
		if($menu){
			$menu=json_decode($menu,true);
			$params['menu']=$menu;
		}
		if($params['is_wz']){
			$open_user=pdo_get('core_open_user',array('id'=>$_SESSION['openid']));
			if($open_user){
				$params['uniacid']=$open_user['open_uid'];
				$params['siteroot']=pdo_getcolumn('core_open',array('id'=>$open_user['open_id']),'siteroot');
				if(empty($params['siteroot'])){
					exi('请先设置第三方平台域名,联系管理员设置！','error','goback');
				}
			}else{
				exi('非第三方进入，不可上传支持第三方的小程序！','error','goback');
			}
			
		}
		$local_blocks=pdo_getall('core_mobile_blocks',array('madebaiduapp <>'=>''),array('cloud_id','default','madebaiduapp'),'cloud_id');
		if($local_blocks){
			foreach($local_blocks as &$row){
				$row['madebaiduapp']=cfc('files')->source_read($row['madebaiduapp']);
				$row['default']=$row['default']?json_decode($row['default'],true):array();
			}
		}
		$post=array('params'=>base64_encode(ijson_encode($params)),'local_blocks'=>$local_blocks);
		$res=cloud_request('wxapp.baidu_release.new',$post);
		exi('打包成功,请下载发布！','','/baiduapp.up_list/');
		//exi($res);
}
